package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.TbUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Objects;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Reference
    private TbUserService tbUserService;

    @RequestMapping("getMessage")
    @ResponseBody
    public Result getMessage(TbUser tbUser){
        rocketMQTemplate.convertAndSend("get_message",tbUser.getPhone());
        return Result.build(ConstantEnum.SUCCESS);
    }


    @RequestMapping("getRegisterInfo")
    @ResponseBody
    public Result getRegisterInfo(TbUser tbUser,String message){
        if (StringUtils.isBlank(message)){
            return Result.build(ConstantEnum.VERIFICATION_CODE);
        }
        String m1 = (String) redisTemplate.opsForValue().get(tbUser.getPhone());
        if (!Objects.equals(message,m1)){
            return Result.build(ConstantEnum.VERIFICATION_CODE_ERROR);
        }
        // ShiroUtils 工具类提供
        String s = ShiroUtils.generateSalt(6);
        //  参数 一.加密方式  参数2.加密字段  参数3.
        String md5 = ShiroUtils.encryptPassword("MD5", tbUser.getPassword(), s, 3);
        tbUser.setSalt(s);
        tbUser.setPassword(md5);
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        tbUserService.getRegisterInfo(tbUser);
        return Result.build(ConstantEnum.SUCCESS_FULLY);
    }


    @RequestMapping("getLoginInfo")
    @ResponseBody
    public Result getLoginInfo(String username, String password, Model model){

        // 获取subject对象
        Subject subject = SecurityUtils.getSubject();
        // 将账号封装成 Token
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            // 调用 subject 登录方法
            subject.login(token);
        }catch (UnknownAccountException e){
            model.addAttribute("message","账号未注册在");
            System.out.println("账号不存在");
            return Result.build(ConstantEnum.REGISTER_ERROR);
        }catch (IncorrectCredentialsException e){
            model.addAttribute("message","密码错误");
            System.out.println("密码错误");
            return Result.build(ConstantEnum.PWD_ERROR);
        }
        return Result.build(ConstantEnum.LOGIN_SUCCESS);
    }
}
